﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Innova.Entites;
using Innova.BusinessLogic;
using Microsoft.Practices.Unity;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Innova.Movil.Controllers
{
    public class getFotoController : Controller
    {
        [Dependency]
        public INewsBL newsBL { get; set; }

        //public FileStreamResult Imagen(int id = 0)
        //{
        //    const string alternativePicturePath = @"/Content/images/ajax-loader.gif";
            
        //    var news = newsBL.GetFromNewsById(id);

        //    MemoryStream stream;

        //    if (news != null && news.Imagen != null)
        //    {
        //        stream = new MemoryStream(news.Imagen);
        //    }
        //    else
        //    {
        //        stream = new MemoryStream();

        //        var path = Server.MapPath(alternativePicturePath);
        //        var image = new System.Drawing.Bitmap(path);

        //        image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
        //        stream.Seek(0, SeekOrigin.Begin);
        //    }

        //    return new FileStreamResult(stream, "image/jpeg");  
            
        //}

        public ActionResult Imagen(int id =  0)
        {

            var news = newsBL.GetFromNewsById(id);

            if (news != null)
            {
                MemoryStream ms = new MemoryStream(news.Imagen);
                var img = Image.FromStream(ms);

                Response.Clear();
                Response.ContentType = "image/png";
                Response.BufferOutput = true;

                img.Save(Response.OutputStream, ImageFormat.Png);
                Response.Flush();

                return View();
            }
            else
                return RedirectToAction("Index");

        }

        //public byte[] ResizeImageFile(byte[] imageFile, int targetSize)
        //{
        //    System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
        //    int targetH, targetW;
        //    if (original.Height > original.Width)
        //    {
        //        targetH = targetSize;
        //        targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
        //    }
        //    else
        //    {
        //        targetW = targetSize;
        //        targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
        //    }
        //    System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
        //    // Create a new blank canvas.  The resized image will be drawn on this canvas.
        //    Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
        //    bmPhoto.SetResolution(72, 72);
        //    Graphics grPhoto = Graphics.FromImage(bmPhoto);
        //    grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
        //    grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //    grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
        //    grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
        //    // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
        //    MemoryStream mm = new MemoryStream();
        //    bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
        //    original.Dispose();
        //    imgPhoto.Dispose();
        //    bmPhoto.Dispose();
        //    grPhoto.Dispose();
        //    return mm.GetBuffer();
        //}




    }
}
